using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using RSSRadio.Model;

namespace RSSRadio
{
    /// <summary>
    /// Form which displays episode episode for windows mobile
    /// </summary>
	public partial class EpisodeDetailsForm : Form
	{
		private string TmpFile;
		private PodcastInfo Podcast;

		public EpisodeDetailsForm()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Displays HTML about the podcast in a WebBrowser control
		/// </summary>
		/// <param name="podcast"></param>
		public EpisodeDetailsForm(PodcastInfo podcast)
		{
			InitializeComponent();
			this.Podcast = podcast;
			this.Closing += new CancelEventHandler(EpisodeDetailsForm_Closing);

			// create a temporary file with HTML describing the podcast episode
			TmpFile = Path.GetTempFileName();
			StreamWriter writer = File.AppendText(TmpFile);
			writer.Write(podcast.ToMobileHTML());
			writer.Close();

			webBrowser1.Navigate(new Uri("file://" + TmpFile));
		}

        /// <summary>
        /// Form Closing handler.  Removes temp files.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		void EpisodeDetailsForm_Closing(object sender, CancelEventArgs e)
		{	
			// remove temporary file
 			if (TmpFile != null)
				File.Delete(TmpFile);
		}

        /// <summary>
        /// btnPlay_Click handler: Plays a podcast
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		private void btnPlay_Click(object sender, EventArgs e)
		{
			PodcastActions.Play(Podcast.FilePath);
		}

        /// <summary>
        /// btnDelete_Click handler: Deletes a podcast
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		private void btnDelete_Click(object sender, EventArgs e)
		{
            if (MessageBox.Show("Delete Podcast?", null, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                Globals.DownloadedPodcasts.RemoveItem(Podcast);
                this.Close();
            }
		}

	}
}